﻿using maxiaopeng.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace maxiaopeng.ControIIers
{
    public class CustomerController : ApiController
    {
        // GET: api/Customer
        [HttpGet]
        public IHttpActionResult GetAskid(string str, string AskerId)
        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                var list = str.Split(',');
                List<int> listid = new List<int>();
                foreach (var item in list)
                {
                    listid.Add(Convert.ToInt32(item));
                }
                foreach (var item in listid)
                {
                    var the = db.Students.FirstOrDefault(x => x.Id == item);
                    the.AskerId = Convert.ToInt32(AskerId);
                }
                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new { code = 0, msg = "成功！" });
                }
                else
                {
                    return Json(new { code = -1, msg = "失败！" });
                }
            }
        }
        [HttpGet]
        public IHttpActionResult GetPageList()
        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                List<int> rid = new List<int>();
                List<Users> uname = new List<Users>();
                var datalist = db.Roles.SingleOrDefault(p => p.Name == "咨询师");
                var datlist1 = db.UserRoles.Where(p => p.RoleId == datalist.Id).Select(p => new
                {
                    UserId = p.UserId
                });
                foreach (var item in datlist1)
                {
                    rid.Add(Convert.ToInt32(item.UserId));
                }
                foreach (var item1 in rid)
                {
                    uname.Add(db.Users.SingleOrDefault(p => p.Id == item1));
                }
                var lsit3 = uname.Select(p => new { LoginName = p.LoginName, Id = p.Id }).ToList();
                return Json(new { data = lsit3 });
            }
        }
        [HttpGet]
        public IHttpActionResult getStraceList(int page, int limit, string StudentName, string UserId, string UpFollowTime, string EndFollowTime, string FollowType)
        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                var tablelist = db.Netfollows.AsEnumerable();
                var list2 = tablelist.Select(p => new
                {
                    FollowType = p.FollowType

                }).ToList().Distinct();
                if (!String.IsNullOrEmpty(StudentName))
                {
                    tablelist = tablelist.Where(p => p.StudentName.Contains(StudentName));
                }
                if (!String.IsNullOrEmpty(UserId))
                {
                    tablelist = tablelist.Where(p => p.UserId ==Convert.ToInt32(UserId));
                }
                if (!String.IsNullOrEmpty(UpFollowTime))
                {
                    tablelist = tablelist.Where(p => p.FollowTime >= Convert.ToDateTime(UpFollowTime));
                }
                if (!String.IsNullOrEmpty(EndFollowTime))
                {
                    tablelist = tablelist.Where(p => p.FollowTime <= Convert.ToDateTime(EndFollowTime));
                }
                if (!String.IsNullOrEmpty(FollowType))
                {
                    tablelist = tablelist.Where(p => p.FollowType == FollowType);
                }
                var list = tablelist.Select(p => new
                {
                    Id = p.Id,
                    StudentId = p.StudentId,
                    UserId = p.UserId,
                    StudentName = p.StudentName,
                    AskUser = p.UserId,
                    FollowState = p.FollowState,
                    FollowTime = p.FollowTime.ToString(),
                    FollowType = p.FollowType,
                    Content = p.Content1,
                    NextFollowTime = p.NextFollowTime.ToString(),
                    CreateTime = p.CreateTime.ToString()
                }).OrderByDescending(p => p.Id).Skip((page - 1) * limit).Take(limit).ToList();

                int count = list.Count();
                return Json(new { count = count, data = list, data2 = list2 });
            }
        }
        //获取该学生追踪日志
        [HttpGet]
        public IHttpActionResult getstulog(int StudentId)
        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                var  stulist = db.Netfollows.Where(p=>p.StudentId==StudentId);
                var list = stulist.AsEnumerable();
                var thelist = list.Select(p=> new
                {
                    Id=p.Id,
                    StudentId = p.StudentId,
                    StudentName = p.StudentName,
                    UserId = p.UserId,
                    Content1 = p.Content1,
                    CreateTime = p.CreateTime,
                    NextFollowTime = p.NextFollowTime.ToString(),
                    FollowTime = p.FollowTime.ToString(),
                    FollowState = p.FollowState,
                    FollowType = p.FollowType,
                }).ToList();
                var count = thelist.Count();
                return Json(new { code = 0, data = thelist, count = count });
            }
        }
        [HttpGet]
        public IHttpActionResult getname(int Id)
        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                var list = db.Students.SingleOrDefault(p => p.Id == Id);
                return Json(new { data = list.Name });
            }
        }
        [HttpGet]
        public IHttpActionResult TypeList()
        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                var list = db.Students.Select(p => new
                {
                    Id = p.Id,
                    Name = p.Name
                }).ToList();
                return Json(new {data=list });
            }
        }
        /// <summary>
        /// 新增网络学生
        /// </summary>
        /// <param name="stu"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult AddStu([FromBody] Students stu)
        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                db.Students.Add(stu);
                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new { code = 0, msg = "学生新增成功" });

                }
                else
                {
                    return Json(new { code = -1, msg = "发生未知错误" });
                }
            }
        }
        /// <summary>
        /// 追踪添加
        /// </summary>
        /// <param name="ask"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult AddDel([FromBody] Netfollows ask)
        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                ask.CreateTime = DateTime.Now;
                db.Netfollows.Add(ask);
                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new { code = 0, msg = "新增成功" });

                }
                else
                {
                    return Json(new { code = -1, msg = "新增失败" });
                }
            }
        }
        [HttpPost]        public IHttpActionResult Edit([FromBody] Students stu)        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                var data = db.Students.SingleOrDefault(p => p.Id == stu.Id);
                data.Addres = stu.Addres;
                data.Name = stu.Name;
                data.Sex = stu.Sex;
                data.Age = stu.Age;
                data.Phone = stu.Phone;
                data.StuStatus = stu.StuStatus;
                data.PerState = stu.PerState;
                data.QQ = stu.QQ;
                data.Weixin = stu.Weixin;
                data.IsValid = stu.IsValid;
                data.IsReturnVist = stu.IsReturnVist;
                data.IsHome = stu.IsHome;
                data.IsPay = stu.IsPay;
                data.Money = stu.Money;
                if (data.IsValid == "是")
                {
                    data.FirstVistitTime = DateTime.Now;
                }
                if (data.IsHome == "是")
                {
                    data.HomeTime = DateTime.Now;
                }
                if (data.IsPay == "是")
                {
                    data.PayTime = DateTime.Now;
                }
                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new { code = 0, msg = "编辑成功" });
                }
                else
                {
                    return Json(new { code = -1, msg = "编辑失败" });
                }
            }
        }
        [HttpGet]
        public IHttpActionResult gettablelist([FromBody] int page, int limit, string Name, string lsPay, string CreateUpTime, string CreateDownTime, int Id)
        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                    var tablelist = db.Students.AsEnumerable();
                    if (!String.IsNullOrEmpty(Name))
                    {
                        tablelist = tablelist.Where(p => p.Name.Contains(Name));
                    }
                    if (!String.IsNullOrEmpty(lsPay))
                    {
                        tablelist = tablelist.Where(p => p.IsPay == lsPay);
                    }
                    if (!String.IsNullOrEmpty(CreateUpTime))
                    {
                        tablelist = tablelist.Where(p => p.CreateTime >= Convert.ToDateTime(CreateUpTime));
                    }
                    if (!String.IsNullOrEmpty(CreateDownTime))
                    {
                        tablelist = tablelist.Where(p => p.CreateTime <= Convert.ToDateTime(CreateDownTime));
                    }
                    var lists = tablelist.Select(p => new
                    {
                        Id = p.Id,
                        Name = p.Name,
                        Phone = p.Phone,
                        Money = p.Money,
                        StuStatus = p.StuStatus,
                        IsPay = p.IsPay,
                        Sex = p.Sex,
                        Age = p.Age,
                        Addres = p.Addres,
                        QQ = p.QQ,
                        WeiXin = p.Weixin,
                        MsgSource = p.MsgSource,
                        AskerId = p.AskerId,
                        IsValid = p.IsValid,
                        NetPusherId = p.NetPusherId,
                        IsReturnVist = p.IsReturnVist,
                        FirstVisitTime = p.FirstVistitTime.ToString(),
                        IsHome = p.IsHome,
                        HomeTime = p.HomeTime.ToString(),
                        PayTime = p.PayTime.ToString(),
                        lsPay = p.IsPay,
                        IsInClass = p.IsInClass,
                        InClassTime = p.InClassTime.ToString(),
                        PreMoneyTime = p.PrdMoneyTime.ToString(),
                        PreMoney = p.PreMoney
                    }).OrderByDescending(p => p.Id).Skip((page - 1) * limit).Take(limit).ToList();
                    int count1 = lists.Count();
                    return Json(new { count = count1, data = lists});
                
            }
        }
        [HttpPost]        public IHttpActionResult Del([FromBody]Students Id)        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                var data = db.Students.SingleOrDefault(p => p.Id == Id.Id);
                db.Students.Remove(data);
                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new { code = 0, msg = "删除成功" });
                }
                else
                {
                    return Json(new { code = -1, msg = "删除失败" });
                }
            }
        }
    }
}
